//
//  GYSpinnerView.m
//  GYCustomHUD
//
//  Created by zgy on 2025/5/6.
//

#import "GYSpinnerView.h"

@implementation GYSpinnerView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupReplicatorLayer];
    }
    return self;
}

- (void)setupReplicatorLayer
{
    // 创建CAReplicatorLayer
    CAReplicatorLayer *replicatorLayer = [CAReplicatorLayer layer];
    replicatorLayer.frame = self.bounds;
    [self.layer addSublayer:replicatorLayer];

    // 设置复制份数
    NSInteger instanceCount = 8;
    replicatorLayer.instanceCount = instanceCount;

    // 设置旋转角度
    CGFloat angle = 2 * M_PI / instanceCount;
    replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1);

    // 设置透明度延迟
    replicatorLayer.instanceAlphaOffset = -0.8 / instanceCount;

    // 创建子图层
    CALayer *barLayer = [CALayer layer];
    CGFloat barWidth = self.bounds.size.width / 10;
    CGFloat barHeight = self.bounds.size.height / 4;
    CGFloat gap = self.bounds.size.height / 3; // 中间空隙大小
    barLayer.frame = CGRectMake(0, 0, barWidth, barHeight);
    barLayer.backgroundColor = [UIColor whiteColor].CGColor;
    barLayer.cornerRadius = barWidth / 2; // 设置圆角
    barLayer.position = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2 - barHeight / 2 - gap / 2);
    [replicatorLayer addSublayer:barLayer];

   

    // 添加旋转动画
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = @0;
    rotationAnimation.toValue = @(2 * M_PI);
    rotationAnimation.duration = 2.0;
    rotationAnimation.repeatCount = HUGE_VALF;
    [replicatorLayer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
}

//- (void)dealloc
//{
//    NSLog(@"GYSpinnerView 释放了");
//}

@end
